<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Page extends MX_Controller {

	private $module = 'page';
	private $table = 'page';
	function __construct(){
		parent::__construct();
		$this->load->model($this->module.'_model','model');
		if($this->uri->segment(1)=='admincp'){
			if($this->uri->segment(2)!='login'){
				if(!$this->session->userdata('userInfo')){
					header('Location: '.base_url().'admincp/login');
					return false;
				}
			}
			$this->template->set_template('admincontrolpanel');
			$this->template->write('title','Admin Control Panel');
		}
	}
	/*------------------------------------ Admin Control Panel ------------------------------------*/
	public function admincp_index($start = 0,$limit = 10){		
		$this->load->library('pagination');
		$config['total_rows'] 	= $this->model->getTotalsearchContent();
		$config['base_url'] 	= base_url().'admincp/'.$this->module.'/index/';
		$config['per_page'] 	= $limit;
		$config['num_links'] 	= 1;
		$config['uri_segment']  = 4;
		$this->pagination->initialize($config);
		
		$result = $this->model->getsearchContent($config['per_page'],$start);
		$data = array(
			'module'		=> $this->module,
			'result'		=> $result,
			
		);
		
		$this->template->write_view('content','BACKEND/index',$data);
		$this->template->render();
	}
	
	
	public function admincp_update($id=0){
		$result[0] = array();
		if($id!=0){
			$result = $this->model->getDetailManagement($id);
		}
		
		$parent_cat = $this->model->getParentCat();
		
		$data = array(
			'result'=>$result[0],
			'module'=>$this->module,
			'parent_cat'	=> $parent_cat,
			'id'=>$id
		);
		$this->template->write_view('content','BACKEND/editContent',$data);
		$this->template->render();
	}

	public function admincp_save(){
		if($_POST){
			if($this->model->saveManagement()){
				header('Location: '.base_url().'admincp/'.$this->module);
				return false;
			}
		}
	}
	
	public function admincp_del_all()
	{
		if($_POST)
		{
			if($this->input->post('checkAll') == 'on'){
				$check = $this->input->post('check');
				
				for($i = 0;$i <= count($check);$i++)
				{
					$this->admincp_delete($check[$i]);
				}
			}
		}
	}
	
	public function admincp_delete($id = 0){
		if($id != 0){
			$this->db->where('id',$id);
			if($this->db->delete(PREFIX.$this->table)){
				header('Location: '.base_url().'admincp/'.$this->module);
				return false;
			}
		}
	}

	public function admincp_updateStatus(){
		if($this->input->post('status')==0){
			$status = 1;
		}else{
			$status = 0;
		}
		$data = array(
			'status'=>$status
		);
		$this->db->where('id', $this->input->post('id'));
		$this->db->update(PREFIX.$this->table, $data);
		
		$update = array(
			'status'=>$status,
			'id'=>$this->input->post('id'),
			'module'=>$this->module
		);
		$this->load->view('BACKEND/updateStatus',$update);
	}
	
	public function admincp_detail($id = 0,$start = 0,$limit = 10)
	{	
		$this->load->library('pagination');
		$config['total_rows'] 	= $this->model->getTotalsearchContent();
		$config['base_url'] 	= base_url().'admincp/'.$this->module.'/detail/';
		$config['per_page'] 	= $limit;
		$config['num_links'] 	= 1;
		$config['uri_segment']  = 4;
		$this->pagination->initialize($config);
		
		$id = (int)$id;
		$result = $this->model->getCatDetail($id,$config['per_page'],$start);
		
		$data = array(
			'module'=>$this->module,
			'result'=>$result
		);
		
		$this->template->write_view('content','BACKEND/index',$data);
		$this->template->render();
	}
	/*------------------------------------ End Admin Control Panel --------------------------------*/
}